// pages/communitySystem/detail/detail.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        statusBarHeight: 0,
        detailId: null,
        detail: null,
        nowIndex: 1,
        commentBgShow: false,
        commentBoxShow: false,
        commentInputShow: false,
        commentWord: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            detailId: options.id
        })
        this.getSystemInfo()
        this.getDetail()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    getSystemInfo: function() {
        const that = this
        wx.getSystemInfo({
            success(res) {
                that.setData({
                    statusBarHeight: res.statusBarHeight + 7
                })
            }
        })
    },

    getDetail: function() {
        wx.showLoading({
            title: '加载中'
        })
        const that = this
        let data = {
            id: this.data.detailId,
            paramter: 1
        }
        common.post('Moments/moment_list', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                wx.showToast({
                    title: res.error,
                    icon: 'none',
                    duration: 2000
                })
                return
            }
            that.setData({
                detail: res[0]
            })
        }).catch(function(e) {})
    },

    goBack: function() {
        wx.navigateBack({
            delta: 1
        })
    },

    nowIndexChange: function(e) {
        this.setData({
            nowIndex: e.detail.current + 1
        })
    },

    previewImg: function(e) {
        wx.previewImage({
            current: e.currentTarget.dataset.img, // 当前显示图片的http链接
            urls: this.data.detail.images // 需要预览的图片http链接列表
        })
    },

    praiseClick: function() {
        if (this.data.detail.is_like == 1) {
            return
        }
        const that = this
        let data = {
            moments_id: this.data.detail.id
        }
        common.post('Moments/moment_like', data).then(function(res) {
            if (res.code == 204) {
                wx.showToast({
                    title: res.error,
                    icon: 'none',
                    duration: 2000
                })
                return
            }
            that.getDetail()
        }).catch(function(e) {})
    },

    showCommentInput: function() {
        this.setData({
            commentInputShow: true
        })
    },

    closeCommentInput: function() {
        this.setData({
            commentInputShow: false
        })
    },

    doNothing: function() {
        return
    },

    commentClick: function() {
        this.setData({
            commentBgShow: true,
            commentBoxShow: true
        })
    },

    closeComment: function() {
        const that = this
        this.setData({
            commentBoxShow: false
        })
        setTimeout(function() {
            that.setData({
                commentBgShow: false
            })
        }, 300)
    },

    commentInput: function(e) {
        this.setData({
            commentWord: e.detail.value
        })
    },

    commentSend: function() {
        if (this.data.commentWord == '') {
            wx.showToast({
                title: '请输入您的评论内容',
                icon: 'none',
                duration: 2000
            })
            return
        }
        wx.showLoading({
            title: '请稍后'
        })
        const that = this
        let data = {
            moments_id: this.data.detail.id,
            content: this.data.commentWord
        }
        common.post('Moments/moment_reply', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                wx.showModal({
                    title: '提示',
                    content: res.error,
                    showCancel: false,
                    success(res) {}
                })
                return
            }
            wx.showModal({
                title: '提示',
                content: '评论成功，请等待审核',
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        that.setData({
                            commentWord: '',
                            commentInputShow: false
                        })
                        that.getDetail()
                    }
                }
            })
        }).catch(function(e) {})
    },

    goUserCommunity: function() {
        wx.showToast({
            title: '即将开放，敬请期待',
            icon: 'none',
            duration: 2000
        })
    }
})